/**
 * API接口模块
 * 集中管理所有后端接口调用
 */
import { get, post, put, del } from '@/utils/request.js';

/**
 * 用户认证相关API
 */
const auth = {
  // 用户登录
  login: (data) => post('/auth/login', data),
  
  // 用户注册
  register: (data) => post('/auth/register', data),
  
  // 检查用户名是否存在
  checkUsername: (username) => get('/auth/check/username', { username }),
  
  // 检查邮箱是否存在
  checkEmail: (email) => get('/auth/check/email', { email }),
  
  // 检查手机号是否存在
  checkPhone: (phone) => get('/auth/check/phone', { phone }),
  
  // 获取当前登录用户信息
  getCurrentUser: () => get('/auth/me'),
  
  // 刷新令牌
  refreshToken: (token) => post('/auth/refresh-token', { token }),
  
  // 修改密码
  changePassword: (data) => post('/auth/change-password', data),
  
  // 重置密码
  resetPassword: (data) => post('/auth/reset-password', data)
};

/**
 * 图书推荐相关API
 */
const recommendation = {
  // 获取热门图书推荐
  getHotBooks: (params) => get('/recommendations/hot', params),
  
  // 获取分类下的热门图书
  getHotBooksByCategory: (categoryId, params) => get(`/recommendations/hot/category/${categoryId}`, params),
  
  // 获取新上架图书
  getNewBooks: (params) => get('/recommendations/new', params),
  
  // 获取分类下的新上架图书
  getNewBooksByCategory: (categoryId, params) => get(`/recommendations/new/category/${categoryId}`, params),
  
  // 获取个性化推荐
  getPersonalRecommendations: (userId, params) => get(`/recommendations/personal/${userId}`, params),
  
  // 获取相似图书
  getSimilarBooks: (bookId, params) => get(`/recommendations/similar/${bookId}`, params),
  
  // 获取作者其他作品
  getAuthorOtherBooks: (authorId, params) => get(`/recommendations/author/${authorId}`, params)
};

/**
 * 数据分析相关API
 */
const analysis = {
  // 获取销量排行数据
  getSalesRanking: (params) => get('/analysis/sales-ranking', params),
  
  // 获取评分分布数据
  getRatingDistribution: (params) => get('/analysis/rating-distribution', params),
  
  // 获取热门关键词
  getHotKeywords: (params) => get('/analysis/hot-keywords', params),
  
  // 获取市场趋势数据
  getMarketTrend: (params) => get('/analysis/market-trend', params)
};

/**
 * 图书分类相关API
 */
const category = {
  // 获取所有分类
  getAllCategories: () => get('/categories'),
  
  // 获取分类详情
  getCategoryDetail: (id) => get(`/categories/${id}`),
  
  // 获取分类下的图书
  getCategoryBooks: (id, params) => get(`/categories/${id}/books`, params)
};

/**
 * 图书相关API
 */
const book = {
  // 获取图书列表
  getBooks: (params) => get('/books', params),
  
  // 获取图书详情
  getBookDetail: (id) => get(`/books/${id}`),
  
  // 搜索图书
  searchBooks: (params) => get('/books/search', params),
  
  // 获取图书评论
  getBookComments: (id, params) => get(`/books/${id}/comments`, params)
};

/**
 * 用户行为相关API
 */
const behavior = {
  // 记录浏览行为
  recordBrowse: (data) => post('/behaviors/browse', data),
  
  // 收藏图书
  addFavorite: (data) => post('/favorites', data),
  
  // 取消收藏
  removeFavorite: (id) => del(`/favorites/${id}`),
  
  // 获取用户收藏列表
  getUserFavorites: (userId, params) => get(`/users/${userId}/favorites`, params),
  
  // 添加评论
  addComment: (data) => post('/comments', data),
  
  // 删除评论
  removeComment: (id) => del(`/comments/${id}`),
  
  // 获取用户评论列表
  getUserComments: (userId, params) => get(`/users/${userId}/comments`, params),
  
  // 获取用户阅读历史
  getUserReadHistory: (userId, params) => get(`/users/${userId}/read-history`, params)
};

// API调用失败时的错误处理已在请求工具中统一处理

// 导出所有API
export default {
  auth,
  recommendation,
  analysis,
  category,
  book,
  behavior
};
